{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Remove Outermost Parentheses"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #stack #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #栈 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: removeOuterParentheses"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #删除最外层的括号"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>有效括号字符串为空 <code>\"\"</code>、<code>\"(\" + A + \")\"</code> 或 <code>A + B</code> ，其中 <code>A</code> 和 <code>B</code> 都是有效的括号字符串，<code>+</code> 代表字符串的连接。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>例如，<code>\"\"</code>，<code>\"()\"</code>，<code>\"(())()\"</code> 和 <code>\"(()(()))\"</code> 都是有效的括号字符串。</li>\n",
    "</ul>\n",
    "\n",
    "<p>如果有效字符串 <code>s</code> 非空，且不存在将其拆分为 <code>s = A + B</code> 的方法，我们称其为<strong>原语（primitive）</strong>，其中 <code>A</code> 和 <code>B</code> 都是非空有效括号字符串。</p>\n",
    "\n",
    "<p>给出一个非空有效字符串 <code>s</code>，考虑将其进行原语化分解，使得：<code>s = P_1 + P_2 + ... + P_k</code>，其中 <code>P_i</code> 是有效括号字符串原语。</p>\n",
    "\n",
    "<p>对 <code>s</code> 进行原语化分解，删除分解中每个原语字符串的最外层括号，返回 <code>s</code> 。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"(()())(())\"\n",
    "<strong>输出：</strong>\"()()()\"\n",
    "<strong>解释：\n",
    "</strong>输入字符串为 \"(()())(())\"，原语化分解得到 \"(()())\" + \"(())\"，\n",
    "删除每个部分中的最外层括号后得到 \"()()\" + \"()\" = \"()()()\"。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"(()())(())(()(()))\"\n",
    "<strong>输出：</strong>\"()()()()(())\"\n",
    "<strong>解释：</strong>\n",
    "输入字符串为 \"(()())(())(()(()))\"，原语化分解得到 \"(()())\" + \"(())\" + \"(()(()))\"，\n",
    "删除每个部分中的最外层括号后得到 \"()()\" + \"()\" + \"()(())\" = \"()()()()(())\"。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"()()\"\n",
    "<strong>输出：</strong>\"\"\n",
    "<strong>解释：</strong>\n",
    "输入字符串为 \"()()\"，原语化分解得到 \"()\" + \"()\"，\n",
    "删除每个部分中的最外层括号后得到 \"\" + \"\" = \"\"。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= s.length <= 10<sup>5</sup></code></li>\n",
    "\t<li><code>s[i]</code> 为 <code>'('</code> 或 <code>')'</code></li>\n",
    "\t<li><code>s</code> 是一个有效括号字符串</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [remove-outermost-parentheses](https://leetcode.cn/problems/remove-outermost-parentheses/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [remove-outermost-parentheses](https://leetcode.cn/problems/remove-outermost-parentheses/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"(()())(())\"', '\"(()())(())(()(()))\"', '\"()()\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res, level = \"\", 0\n",
    "        for c in s:\n",
    "            if c == ')':\n",
    "                level -= 1\n",
    "            if level:\n",
    "                res += c\n",
    "            if c == '(':\n",
    "                level += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        rs=[]\n",
    "        rd={}\n",
    "        rd['('],rd[')']=0,0\n",
    "        i,j=0,0\n",
    "        while i < len(s):        \n",
    "            rd[s[i]]+=1\n",
    "            if rd['(']==rd[')']:\n",
    "                #print(i,j,s[j:i])\n",
    "                rs.append(s[j+1:i])\n",
    "                rd['('],rd[')']=0,0\n",
    "                j=i+1\n",
    "            i+=1                       \n",
    "        #print(rs)\n",
    "        return ''.join(rs)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        del_idx = set()\n",
    "        stack = []\n",
    "        for i, c in enumerate(s):\n",
    "            if c == ')':\n",
    "                if len(stack) == 1:\n",
    "                    del_idx.add(i)\n",
    "                    del_idx.add(stack[-1])\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(i)\n",
    "        return ''.join([c for i, c in enumerate(s) if i not in del_idx])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        #左括号入栈，如果入栈后长度不是1则不是第一个，结果加入右括号，若出栈后大于0则右也不是最后一个加入res\n",
    "        stack=[]\n",
    "        res=''\n",
    "        for i in s:\n",
    "            if i=='(':\n",
    "                stack.append(i)\n",
    "                if len(stack)>1:\n",
    "                    res+='('\n",
    "            else:\n",
    "                stack.pop()\n",
    "                if len(stack)!=0:\n",
    "                    res+=')'\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        ans=\"\"\n",
    "        l=\"\"\n",
    "        p=0\n",
    "        for i in s:\n",
    "            if i==\"(\":\n",
    "                p=p+1\n",
    "            else:\n",
    "                p=p-1\n",
    "            l=l+i\n",
    "            if p==0:\n",
    "                if len(l)>2:\n",
    "                    ans=ans+l[1:len(l)-1]\n",
    "                l=\"\"\n",
    "        return ans\n",
    "\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res, stack = \"\", []\n",
    "        for i in s:\n",
    "            if i == \"(\":\n",
    "                if stack: res += i\n",
    "                stack.append(\"(\")\n",
    "            if i == \")\":\n",
    "                stack.pop()\n",
    "                if stack: res += i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        num = 1\n",
    "        ss = \"\"\n",
    "        for e in s:\n",
    "            if e == \"(\":\n",
    "                if num > 1:\n",
    "                    ss += e\n",
    "                num += 1\n",
    "            if e == \")\":\n",
    "                num -= 1\n",
    "                if num > 1:\n",
    "                    ss += e\n",
    "        return ss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res = \"\"\n",
    "        stack = []\n",
    "        for c in s:\n",
    "            # 如果是右括号，就出栈\n",
    "            if c == ')':\n",
    "                stack.pop()\n",
    "            # 如果stack不为空，则表示可以加上去\n",
    "            if stack:\n",
    "                res += c\n",
    "            # 左括号，入栈\n",
    "            if c == '(':\n",
    "                stack.append(c)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        n = 0 \n",
    "        res = ''\n",
    "        for i in s:\n",
    "            if i == '(':\n",
    "                n += 1\n",
    "                if n > 1:\n",
    "                    res += i\n",
    "            else:\n",
    "                n -= 1\n",
    "                if n > 0:\n",
    "                    res += i\n",
    "                \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        left = 0\n",
    "        right = 0\n",
    "        pro_str = ''\n",
    "        primi_str = ''\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == '(':\n",
    "                left += 1\n",
    "            else:\n",
    "                right += 1\n",
    "            pro_str += s[i]\n",
    "            if left == right:\n",
    "                    primi_str += (pro_str[1:-1])\n",
    "                    pro_str = ''\n",
    "\n",
    "        return primi_str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        length = len(s)\n",
    "        output,yuanyu = \"\",\"\"\n",
    "        left = 0\n",
    "        for i in range(length):  #\n",
    "            yuanyu += s[i]\n",
    "            if s[i] == '(':\n",
    "                left += 1\n",
    "            elif s[i] == ')':\n",
    "                left -= 1\n",
    "            if left == 0:\n",
    "                output += yuanyu[1:-1]\n",
    "                yuanyu = \"\"\n",
    "        return output\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str: \n",
    "        stack = []\n",
    "        res = \"\"\n",
    "        for symbolic in s:\n",
    "            if symbolic == \")\":\n",
    "                stack.pop()\n",
    "            if stack:\n",
    "                res += symbolic\n",
    "            if symbolic == \"(\":\n",
    "                stack.append(symbolic)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        n, cnt = len(s), 0\n",
    "        ans = ''\n",
    "        for i, c in enumerate(s):\n",
    "            if (c == '(' and cnt > 0) or (c == ')' and cnt > 1):\n",
    "                ans += c\n",
    "            cnt += 1 if c == '(' else -1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        # stack = []\n",
    "        # ans = []\n",
    "        # for ch in s:\n",
    "        #     if stack and stack[-1] == \"(\" and ch == \")\":\n",
    "        #         stack.pop()\n",
    "        #     if len(stack) == 1:\n",
    "        #         ans.append(\"()\")\n",
    "        #     if ch == \"(\":\n",
    "        #         stack.append(ch)\n",
    "\n",
    "\n",
    "        res, stack = \"\", []\n",
    "        for c in s:\n",
    "            if c == ')':\n",
    "                stack.pop()\n",
    "            if stack:\n",
    "                res += c\n",
    "            if c == '(':\n",
    "                stack.append(c)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res = ''\n",
    "        stack = []\n",
    "        for c in s:\n",
    "            if c == '(':\n",
    "                stack.append(c)\n",
    "                if len(stack) > 1:\n",
    "                    res += c\n",
    "            else:\n",
    "                stack.pop()\n",
    "                if len(stack) > 0:\n",
    "                    res += c\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        stack = []\n",
    "        result = ''\n",
    "        for str in s:\n",
    "            if str == '(':\n",
    "                stack.append(str)\n",
    "                if len(stack) > 1:\n",
    "                    result += '('\n",
    "            else:\n",
    "                stack.pop()\n",
    "                if len(stack) != 0:\n",
    "                    result += ')'\n",
    "        return result "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res, stack = \"\",[]\n",
    "        for c in s:\n",
    "            if c == ')':\n",
    "                stack.pop()\n",
    "            if stack:\n",
    "                res += c\n",
    "            if c == '(':\n",
    "                stack.append(c)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res, stack = \"\", []\n",
    "        for c in s:\n",
    "            if c == ')':\n",
    "                stack.pop()\n",
    "            if stack:\n",
    "                res += c\n",
    "            if c == '(':\n",
    "                stack.append(c)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res, stack = \"\", []\n",
    "        for c in s:\n",
    "            if c == ')':\n",
    "                stack.pop()\n",
    "            if stack:\n",
    "                res += c \n",
    "            if c == '(':\n",
    "                stack.append(c)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        Stack=[]   # \"(()())(())\"\n",
    "        res=''\n",
    "        dic={'(':')'}\n",
    "        for i in s:\n",
    "            if i=='(' and Stack==[]:\n",
    "                Stack.append('(')\n",
    "            elif i=='(' and Stack!=[]:\n",
    "                Stack.append('(')\n",
    "                res+=i\n",
    "            elif i==')' and Stack!=[]:\n",
    "                if dic[Stack[-1]]==i:\n",
    "                    Stack.pop()\n",
    "                    if Stack==[]:\n",
    "                        continue\n",
    "                    else:\n",
    "                        res+=i\n",
    "                else:\n",
    "                    res+=i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        dic = []\n",
    "        ans = \"\"\n",
    "        for x in s:\n",
    "            if x == \"(\":\n",
    "                dic.append(x)\n",
    "            else:\n",
    "                res = \")\"\n",
    "                while dic[-1] != \"(\":\n",
    "                    res = dic.pop() + res\n",
    "                res = dic.pop() + res\n",
    "                if not dic:\n",
    "                    res = res[1:-1]\n",
    "                    ans += res\n",
    "                else:\n",
    "                    dic.append(res)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        dic = []\n",
    "        ans = \"\"\n",
    "        for x in s:\n",
    "            if x == \"(\":\n",
    "                dic.append(x)\n",
    "            else:\n",
    "                res = \")\"\n",
    "                while dic[-1] != \"(\":\n",
    "                    res = dic.pop() + res\n",
    "                res = dic.pop() + res\n",
    "                if not dic:\n",
    "                    res = res[1:-1]\n",
    "                    ans += res\n",
    "                else:\n",
    "                    dic.append(res)\n",
    "        return ans\n",
    "        # dic = {\"(\":0, \")\":0}\n",
    "        # ans = \"\"\n",
    "        # i0 = 0\n",
    "        # s = s + \"(\"\n",
    "        # for i, x in enumerate(s):\n",
    "        #     if i > 0 and dic[\"(\"] == dic[\")\"]:\n",
    "        #         ans += s[i0+1:i-1]\n",
    "        #         i0 = i\n",
    "        #         dic[x] += 1\n",
    "        #     else:\n",
    "        #         dic[x] += 1\n",
    "        # return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res, stack = \"\", []\n",
    "        for c in s:\n",
    "            if c == ')':\n",
    "                stack.pop()\n",
    "            if stack:\n",
    "                res += c\n",
    "            if c == '(':\n",
    "                stack.append(c)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        c = 0\n",
    "        ans,t = '',''\n",
    "        for i in s:\n",
    "            if i == '(':\n",
    "                c += 1\n",
    "            else:\n",
    "                c -= 1\n",
    "            if i == ')' and c == 0:\n",
    "                ans += t[1:len(t)]\n",
    "                t = ''\n",
    "            else:\n",
    "                t += i \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res, stack = \"\", []\n",
    "        for c in s:\n",
    "            if c == \")\":\n",
    "                stack.pop()\n",
    "            if stack:\n",
    "                res += c\n",
    "            if c == \"(\":\n",
    "                stack.append(c)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res,stack=\"\",[]\n",
    "        for token in s:\n",
    "            if token==\"(\":\n",
    "                if stack:\n",
    "                    res+=token\n",
    "                stack.append(\"(\")\n",
    "            if token==\")\":\n",
    "                stack.pop()\n",
    "                if stack:\n",
    "                    res+=token\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res = \"\"\n",
    "        stack = []\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == \"(\":\n",
    "                stack.append(i)\n",
    "            else:\n",
    "                left = stack.pop()\n",
    "                if not stack:\n",
    "                    res += s[left+1:i]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res, stack = \"\", []\n",
    "        for c in s:\n",
    "            if c == ')':\n",
    "                stack.pop()\n",
    "            if stack:\n",
    "                res += c\n",
    "            if c == '(':\n",
    "                stack.append(c)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res = ''\n",
    "        stack = []\n",
    "        for i in s:\n",
    "            if i == ')':\n",
    "                stack.pop()\n",
    "            if stack:\n",
    "                res += i\n",
    "            if i == '(':\n",
    "                stack.append(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        l=0\n",
    "        r=0\n",
    "        ss=\"\"\n",
    "        ans=\"\"\n",
    "        for i in s:\n",
    "            ss+=i\n",
    "            if i==\"(\":\n",
    "                l+=1\n",
    "            if i==\")\":\n",
    "                r+=1\n",
    "            if l==r:\n",
    "                ans+=ss[1:-1]\n",
    "                ss=\"\"\n",
    "                l=0\n",
    "                r=0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        count = 0\n",
    "        mid = ''\n",
    "        out = ''\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == '(':\n",
    "                count += 1\n",
    "            elif s[i] == ')':\n",
    "                count -= 1\n",
    "            mid += s[i]\n",
    "            if count == 0:\n",
    "                out += mid[1:-1]\n",
    "                mid = ''\n",
    "        return out\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        ans = \"\"\n",
    "        i, count = 0, 0\n",
    "        for j in range(len(s)):\n",
    "            if s[j] == \"(\":\n",
    "                count += 1\n",
    "            else:\n",
    "                count -= 1\n",
    "            if count == 0:\n",
    "                ans += s[i + 1: j]\n",
    "                i = j + 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res, stack = \"\", []\n",
    "        for c in s:\n",
    "            if c == ')':\n",
    "                stack.pop()\n",
    "            if stack:\n",
    "                res += c\n",
    "            if c == '(':\n",
    "                stack.append(c)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def removeOuterParentheses(self, s: str) -> str:\r\n",
    "        ans = list()\r\n",
    "        temp = list()\r\n",
    "        queue = list()\r\n",
    "        for i in s:\r\n",
    "            temp.append(i)\r\n",
    "            if i == ')':\r\n",
    "                queue.pop(0)\r\n",
    "            else:\r\n",
    "                queue.append(i)\r\n",
    "            if not queue:\r\n",
    "                ans.append(''.join(temp[1:-1]))\r\n",
    "                temp = list()\r\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        ans = ''\n",
    "        start, n = 0, len(s)\n",
    "        while start < n:\n",
    "            end = start + 1\n",
    "            cnt = 1\n",
    "            while cnt > 0:\n",
    "                if s[end] == '(':\n",
    "                    cnt += 1\n",
    "                else:\n",
    "                    cnt -= 1\n",
    "                end += 1\n",
    "            ans += s[start+1:end-1]\n",
    "            start = end\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        ans = []\n",
    "        # 大于一层左括号时加入输出\n",
    "        l = 0\n",
    "        for c in s:\n",
    "            if c == '(':\n",
    "                l += 1\n",
    "            if l > 1:\n",
    "                ans.append(c)\n",
    "            if c == ')':\n",
    "                l -= 1\n",
    "\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        if not s:\n",
    "            return \"\"\n",
    "        res = \"\"\n",
    "        stack = []\n",
    "        for c in s:\n",
    "            if c == \"(\":\n",
    "                stack.append(c)\n",
    "                if len(stack) >1:\n",
    "                    res += \"(\"\n",
    "            else:\n",
    "                stack.pop()\n",
    "                if stack:\n",
    "                    res += \")\"\n",
    "        return res\n",
    "\n",
    "if __name__==\"__main__\":\n",
    "    s = \"(()())(())\"\n",
    "    obj = Solution()\n",
    "    print(obj.removeOuterParentheses(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "                ans=''\n",
    "\n",
    "                stack=[]\n",
    "                l,r=0,0\n",
    "                for i in s:\n",
    "                    stack.append(i)\n",
    "                    if i=='(':\n",
    "                        l+=1\n",
    "                    if i==')':\n",
    "                        r+=1\n",
    "                    if stack and l==r:\n",
    "                        for i in stack[1:-1]:\n",
    "                            ans=ans+str(i)\n",
    "                        stack=[]\n",
    "                return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        l=[]\n",
    "        m,n=0,0\n",
    "        a=0\n",
    "        for j,i in enumerate(s):\n",
    "            if i=='(':\n",
    "                m+=1\n",
    "            elif i==')':\n",
    "                n+=1\n",
    "            if m==n and m>0:\n",
    "                l.append(s[a+1:j])\n",
    "                a=j+1\n",
    "        ans=''\n",
    "        for i in l:\n",
    "            ans+=i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        start = 0\n",
    "        left = 0\n",
    "        right = 0\n",
    "        sb = ''\n",
    "        for i, x in enumerate(s):\n",
    "            if x == '(':\n",
    "                left += 1\n",
    "            else:\n",
    "                right += 1\n",
    "            if left == right:\n",
    "                sb = sb + s[start + 1:i]\n",
    "                start = i + 1\n",
    "                left = 0\n",
    "                right = 0\n",
    "        return sb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        stack = []\n",
    "        res = ''\n",
    "        for c in s:\n",
    "            if c =='(':\n",
    "                stack.append(c)\n",
    "                if len(stack) > 1:\n",
    "                    res += '('\n",
    "            else:\n",
    "                stack.pop()\n",
    "                if stack:\n",
    "                    res += ')'\n",
    "        return res\n",
    "\n",
    "if __name__==\"__main__\":\n",
    "    s = \"(()())(())\"\n",
    "    obj = Solution()\n",
    "    print(obj.removeOuterParentheses(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str: \n",
    "        stack = []\n",
    "        res = []\n",
    "        sub = \"\"\n",
    "        for symbolic in s:\n",
    "            sub += symbolic\n",
    "            if symbolic == \"(\":\n",
    "                stack.append(symbolic)\n",
    "            else:\n",
    "                stack.pop()\n",
    "            if not stack:\n",
    "                res.append(sub[1:-1])\n",
    "                sub = \"\"\n",
    "        return \"\".join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        ans=[]\n",
    "        stack=[]\n",
    "        i=0\n",
    "        n=len(s)\n",
    "        while i<n:\n",
    "            if s[i]==\"(\":\n",
    "                stack.append(s[i])\n",
    "            else:\n",
    "                stack.pop()\n",
    "        \n",
    "            if stack==[] and i!=0:\n",
    "                ans.append(i)\n",
    "            i=i+1\n",
    "        res=\"\"\n",
    "        ans.append(n-1)\n",
    "        l=0\n",
    "        for i in range(len(ans)-1):\n",
    "            r=ans[i]\n",
    "            tmp=s[l+1:r]\n",
    "            l=r+1\n",
    "            res+=tmp\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        res = ''\n",
    "        Sum = 0\n",
    "        k = 0\n",
    "        for i in s:\n",
    "            if i == ')':\n",
    "                Sum -= 1\n",
    "            if Sum:\n",
    "                res += i\n",
    "            if i == '(':\n",
    "                Sum += 1\n",
    "        return res\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        start = 0\n",
    "        left = 0\n",
    "        right = 0\n",
    "        sb = ''\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == '(':\n",
    "                left += 1\n",
    "            else:\n",
    "                right += 1\n",
    "            if left == right:\n",
    "                sb = sb + s[start + 1:i]\n",
    "                start = i + 1\n",
    "                left = 0\n",
    "                right = 0\n",
    "\n",
    "        return sb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        index_stack = []\n",
    "        index_cnt = 0\n",
    "        s = list(s)\n",
    "        for s_index in range(len(s)):\n",
    "            if s[s_index] == ')':\n",
    "                index_cnt -=1\n",
    "            if index_cnt == 0:\n",
    "                index_stack.append(s_index)\n",
    "            if s[s_index] == '(':\n",
    "                index_cnt +=1\n",
    "        # s = list(s)\n",
    "        index_stack.reverse()\n",
    "        for index in index_stack:\n",
    "            del s[index]        \n",
    "        return ''.join(s)    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        if not s:\n",
    "            return \"\"\n",
    "        res = \"\"\n",
    "        stack = []\n",
    "        for c in s:\n",
    "            if c == \"(\":\n",
    "                stack.append(c)\n",
    "                if len(stack) > 1:\n",
    "                    res += \"(\"\n",
    "            else:\n",
    "                stack.pop()\n",
    "                if stack:\n",
    "                    res += \")\"\n",
    "        return res\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    s = \"(()())(())\"\n",
    "    obj = Solution()\n",
    "    print(obj.removeOuterParentheses(s))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        l = 0\n",
    "        res = ''\n",
    "        for c in s:\n",
    "            if c == '(':\n",
    "                l += 1\n",
    "                if l > 1:\n",
    "                    res += c\n",
    "            else:\n",
    "                if l > 1:\n",
    "                    res += c\n",
    "                l -= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        cur=0\n",
    "        diff=0\n",
    "        start=0\n",
    "        result=''\n",
    "        for i in range(len(s)):\n",
    "            if s[i]=='(':\n",
    "                diff+=1\n",
    "            else:\n",
    "                diff-=1\n",
    "                if diff==0:\n",
    "                    result+=s[start+1:i]\n",
    "                    start=i+1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res=\"\"\n",
    "        stack=[]\n",
    "        for x in s:\n",
    "            if x==')':\n",
    "                stack.pop()  \n",
    "            if stack!=[]:\n",
    "                res=res+x\n",
    "            if x=='(':\n",
    "                stack.append(x)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res, stack = \"\", []\n",
    "        for c in s:\n",
    "            if c == ')':\n",
    "                stack.pop()\n",
    "            if stack:\n",
    "                res += c\n",
    "            if c == '(':\n",
    "                stack.append(c)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        result = 0\n",
    "        left,right = 0, -1\n",
    "        arr = []\n",
    "        for x in s:\n",
    "            right += 1\n",
    "            if x == '(':\n",
    "                result += 1\n",
    "            elif x == ')':\n",
    "                result -= 1\n",
    "            if result == 0:\n",
    "                arr.append(s[left+1:right])\n",
    "                left = right+1\n",
    "        return ''.join(arr)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res = \"\"\n",
    "        stack = []\n",
    "        for c in s:\n",
    "            if c == ')':\n",
    "                stack.pop()\n",
    "            # 保证第一对括号不被记录\n",
    "            if stack:\n",
    "                res += c\n",
    "            if c == '(':\n",
    "                stack.append(c)\n",
    "        return res\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        result = []\n",
    "        level = 0\n",
    "        for i in range(0, len(s)):\n",
    "            if s[i] == ')':\n",
    "                level -= 1\n",
    "            if level >= 1:\n",
    "                result.append(s[i])\n",
    "            if s[i] == '(':\n",
    "                level += 1\n",
    "        return ''.join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        st = []\n",
    "        res = []\n",
    "        st.append(s[0])\n",
    "        for i in range(1, len(s)):\n",
    "            if st!= []:\n",
    "                if s[i] == \"(\":\n",
    "                    res.append(s[i])\n",
    "                    st.append(s[i])\n",
    "                else:\n",
    "                    st.pop()\n",
    "                    if st!=[]:\n",
    "                        res.append(s[i])\n",
    "            else:\n",
    "                st.append(s[i])\n",
    "        return \"\".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        left = 0\n",
    "        right = 0\n",
    "        res = [-1]\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == '(':\n",
    "                left += 1\n",
    "            else:\n",
    "                right += 1\n",
    "            if left == right:\n",
    "                res.append(i)\n",
    "        if len(res) == 1:\n",
    "            return \"\"\n",
    "        else:\n",
    "            str_res = ''\n",
    "            for i in range(len(res) - 1):\n",
    "                str_res += s[res[i]+2: res[i+1]]\n",
    "            return str_res\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        index_stack = []\n",
    "        index_cnt = 0\n",
    "        s = list(s)\n",
    "        for s_index in range(len(s)):\n",
    "            if s[s_index] == ')':\n",
    "                index_cnt -=1\n",
    "            if index_cnt == 0:\n",
    "                index_stack.append(s_index)\n",
    "            if s[s_index] == '(':\n",
    "                index_cnt +=1\n",
    "        # s = list(s)\n",
    "        index_stack.reverse()\n",
    "        for index in index_stack:\n",
    "            del s[index]        \n",
    "        return ''.join(s)    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        temp = []\n",
    "        start = 0\n",
    "        end = 0\n",
    "        count = 0\n",
    "        while end < len(s):\n",
    "            if s[end] == \"(\":\n",
    "                count += 1\n",
    "            elif s[end] == \")\":\n",
    "                count -= 1\n",
    "            if count == 0:\n",
    "                temp.append(s[start:end+1])\n",
    "                count = 0\n",
    "                start = end + 1\n",
    "            end += 1\n",
    "        res = \"\"\n",
    "        for i in temp:\n",
    "            res += i[1:-1]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        # dic = {')' : '('}\n",
    "        # stack = []\n",
    "        index_stack = []\n",
    "        index_cnt = 0\n",
    "        for s_index in range(len(s)):\n",
    "            if s[s_index] == ')':\n",
    "                index_cnt -=1\n",
    "            if index_cnt == 0:\n",
    "                index_stack.append(s_index)\n",
    "            if s[s_index] == '(':\n",
    "                index_cnt +=1\n",
    "            # else\n",
    "        print(index_stack)\n",
    "        s = list(s)\n",
    "        print(s)\n",
    "        index_stack.reverse()\n",
    "        # for s_index in range(len(s), -1):\n",
    "        # for index in range(len(index_stack), -1):\n",
    "        for index in index_stack:\n",
    "            del s[index]        \n",
    "        print(s)\n",
    "        return ''.join(s)    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        cache = []\n",
    "        primitive_list = []\n",
    "        tmp = ''\n",
    "        for char in s:\n",
    "            if not cache:\n",
    "                cache.append(char)\n",
    "                tmp += char\n",
    "            else:\n",
    "                if cache[-1] == '(' and char ==\")\":\n",
    "                    cache.pop()\n",
    "                    tmp += char\n",
    "                    if not cache:\n",
    "                        primitive_list.append(tmp)\n",
    "                        tmp = ''\n",
    "                else:\n",
    "                    cache.append(char)\n",
    "                    tmp += char\n",
    "        ans = ''\n",
    "        for primitive in primitive_list:\n",
    "            ans += primitive[1:len(primitive)-1]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        t=0\n",
    "        m=[]\n",
    "        for j in range(len(s)):\n",
    "            if s[j]=='(':\n",
    "                t+=1\n",
    "            else:\n",
    "                t-=1\n",
    "            if t==0:\n",
    "                m.append(j)\n",
    "        m=m[:-1]\n",
    "        s=list(s)\n",
    "        for i in m[::-1]:\n",
    "            s.pop(i)\n",
    "            s.pop(i+1)\n",
    "        s.pop(0)\n",
    "        s.pop(-1)\n",
    "        return ''.join(s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        result = []\n",
    "        begin = 0\n",
    "        count = 0\n",
    "        for i, c in enumerate(s):\n",
    "            if c == '(':\n",
    "                count += 1\n",
    "            elif c == ')':\n",
    "                count -= 1\n",
    "            if count == 0:\n",
    "                result.append(s[begin:i+1])\n",
    "                begin = i + 1\n",
    "        result = [x[1:-1] for x in result]\n",
    "        return ''.join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        zhan = []\n",
    "        primitive = []\n",
    "        zhongjianliang = []\n",
    "        for i in s:\n",
    "            if i == '(':\n",
    "                zhan.append(i)\n",
    "            else:\n",
    "                zhan.pop()\n",
    "            zhongjianliang.append(i)\n",
    "            if zhan == []:\n",
    "                primitive.append(''.join(zhongjianliang))\n",
    "                zhongjianliang = []\n",
    "        ans = []\n",
    "        for i in primitive:\n",
    "            i = list(i)\n",
    "            i.pop()\n",
    "            i.pop(0)\n",
    "            ans.append(''.join(i))\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        left_bracket = 0 \n",
    "        primitive = []\n",
    "        tmp = \"\"\n",
    "        for ch in s :\n",
    "            if ch == '(':\n",
    "                left_bracket +=1 \n",
    "            elif ch == ')' :\n",
    "                left_bracket -= 1 \n",
    "            tmp += ch \n",
    "            if left_bracket == 0:\n",
    "                primitive.append(tmp)\n",
    "                tmp = \"\"\n",
    "        for i , p in enumerate(primitive) :\n",
    "            p = p[1:-1]\n",
    "            primitive[i] = p \n",
    "        return \"\".join(primitive)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        temp = []\n",
    "        start = 0\n",
    "        end = 0\n",
    "        count = 0\n",
    "        while end < len(s):\n",
    "            if s[end] == \"(\":\n",
    "                count += 1\n",
    "            elif s[end] == \")\":\n",
    "                count -= 1\n",
    "            if count == 0:\n",
    "                temp.append(s[start:end+1])\n",
    "                count = 0\n",
    "                start = end + 1\n",
    "            end += 1\n",
    "        res = \"\"\n",
    "        for i in temp:\n",
    "            res += i[1:-1]\n",
    "        return res\n",
    "                        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        ans = \"\"\n",
    "        ans_list=[]\n",
    "        ln = 0\n",
    "        rn = 0\n",
    "        i=0\n",
    "        n=len(s)\n",
    "        while i < n:\n",
    "            j=i+1\n",
    "            if s[i] == \"\":\n",
    "                return \"\"\n",
    "            if s[i] == \"(\":\n",
    "                ln+=1\n",
    "                while ln > rn and j < n:\n",
    "                    if s[j] == \"(\":\n",
    "                        ln+=1\n",
    "                    if s[j] == \")\":\n",
    "                        rn+=1\n",
    "                    j+=1\n",
    "                ans_list.append(s[i:j])\n",
    "                i=j\n",
    "        # print(ans_list)\n",
    "        for i in ans_list:\n",
    "            # print(i[1:-1])\n",
    "            ans+=i[1:-1]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        s_list = list(s)\n",
    "        i,t = 0,0\n",
    "        arr,index = [],[]\n",
    "        while i < (len(s_list)):\n",
    "            if s_list[i] == '(':\n",
    "                arr.append(s_list[i])\n",
    "                index.append(i)\n",
    "            else:\n",
    "                if len(arr) == 1:\n",
    "                    index.append(i)\n",
    "                    arr.pop()\n",
    "                else:\n",
    "                    arr.pop()\n",
    "                    index.pop()\n",
    "            i += 1\n",
    "        print(index)\n",
    "        result = []\n",
    "        j = 0\n",
    "        while j < (len(s_list)):\n",
    "            if j in index:\n",
    "                j += 1\n",
    "            else:\n",
    "                result.append(s_list[j])\n",
    "                j += 1\n",
    "\n",
    "        return \"\".join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res = [-1]\n",
    "        stack = list()\n",
    "        chars = []\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == \"(\":\n",
    "                stack.append(s[i])\n",
    "            elif s[i] == \")\":\n",
    "                stack.pop(-1)\n",
    "            if not stack:\n",
    "                res.append(i)\n",
    "        for k in range(1, len(res)):\n",
    "            chars.append(s[res[k-1]+2:res[k]])\n",
    "        return \"\".join(chars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        stack = []\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            if s[i] == \"(\":\n",
    "                #print(\"i:\",i)\n",
    "                stack.append(i)\n",
    "            else:\n",
    "                j = stack.pop()\n",
    "                #print(\"j:\",j)\n",
    "                if len(stack) == 0:\n",
    "                    #print(j)\n",
    "                    res.append(j)\n",
    "        res.append(n)\n",
    "        print(res)\n",
    "\n",
    "        left = 0\n",
    "        right = 1\n",
    "        new = \"\"\n",
    "        while right < len(res):\n",
    "            new += s[res[left]+1:res[right]-1]\n",
    "            left = right\n",
    "            right += 1\n",
    "        return new\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        if not s:\n",
    "            return s\n",
    "        stack=[s[0]]\n",
    "        removed=[]\n",
    "        left=0\n",
    "        pos=1\n",
    "        while pos<len(s):\n",
    "            if s[pos]=='(':\n",
    "                stack.append('(')\n",
    "            else:\n",
    "                stack.pop()\n",
    "                if stack==[]:\n",
    "                    removed.append(left)\n",
    "                    removed.append(pos)\n",
    "                    left=pos+1\n",
    "            pos+=1\n",
    "        new_s=list(s)\n",
    "        for i in removed:\n",
    "            new_s[i]=''\n",
    "        return ''.join(new_s)\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        res = []\n",
    "        left = 0\n",
    "        right = 0\n",
    "        del_num = [0]\n",
    "\n",
    "        for i in range(len(s)):\n",
    "            #res.append(s[i])\n",
    "            if s[i] == \"(\":\n",
    "                left = left + 1\n",
    "            if s[i] == \")\":\n",
    "                right = right + 1\n",
    "            \n",
    "            if left == right:\n",
    "                left = 0\n",
    "                right = 0\n",
    "                del_num.append(i)\n",
    "                if i < len(s) - 1:\n",
    "                    del_num.append(i+1)\n",
    "            \n",
    "        print(del_num)\n",
    "        print(res)\n",
    "        for i in range(len(s)):\n",
    "            if i not in del_num:\n",
    "                res.append(s[i])\n",
    "        res = \"\".join(res)\n",
    "        return res\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        # 将字符串分解成有效的字符串\n",
    "        # 然后对于这些字符串去除外层的括号后，加在一起\n",
    "\n",
    "        sentence = []\n",
    "\n",
    "        left, right = 0, 0\n",
    "        index = 0\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == '(':\n",
    "                left += 1\n",
    "            if s[i] == ')':\n",
    "                right += 1\n",
    "            if left == right:\n",
    "                sentence.append(s[index:i + 1])\n",
    "                index = i + 1\n",
    "                left, right = 0, 0\n",
    "            \n",
    "        for i in range(len(sentence)):\n",
    "            sentence[i] = sentence[i][1:-1]\n",
    "        res = \"\"\n",
    "        for i in sentence:\n",
    "            res += i\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "        stack = list()\n",
    "        res = [-1]\n",
    "        chars = list()\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == \"(\":\n",
    "                stack.append(s[i])\n",
    "            elif s[i] == \")\":\n",
    "                stack.pop(-1)\n",
    "            if not stack:\n",
    "                res.append(i)\n",
    "        for k in range(1, len(res)):\n",
    "            chars.append(s[res[k-1]+2:res[k]])\n",
    "        return \"\".join(chars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeOuterParentheses(self, s: str) -> str:\n",
    "\n",
    "        stack = []\n",
    "        res = []\n",
    "\n",
    "        pair = {\n",
    "            '(' : ')'\n",
    "        }\n",
    "\n",
    "        start = 0\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == \"(\":\n",
    "                stack.append(s[i])\n",
    "            else:\n",
    "                stack.pop()\n",
    "\n",
    "            if i>0 and len(stack) == 0:\n",
    "                res.append(s[start:i+1])\n",
    "                start = i+1\n",
    "\n",
    "        ans = \"\"\n",
    "        for i in range(len(res)):\n",
    "            ans += \"\".join(res[i][1:-1])\n",
    "        return ans\n",
    "            \n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
